package com.hifar.plugins;

import com.deepoove.poi.data.TextRenderData;
import com.deepoove.poi.policy.AbstractRenderPolicy;
import com.deepoove.poi.policy.TextRenderPolicy;
import com.deepoove.poi.render.RenderContext;
import com.tssk.kylx.fw.utils.lang.StringUtils;
import org.apache.poi.xwpf.usermodel.XWPFRun;

import java.util.Objects;

/**
 * @Description: 文本渲染插件  文本为空不删除书签/或设置默认值
 * @Author zhuWeiWei
 * @Date 2022/6/23 16:21
 */
public class TextBlankRenderPolicy extends AbstractRenderPolicy<Object> {

    private String defaultVal = null;

    @Override
    public void doRender(RenderContext<Object> context) throws Exception {
        String data = StringUtils.trimNull(context.getData());
        if (StringUtils.isBlank(data)) {
            data = defaultVal;
        }
        XWPFRun run = context.getRun();
        if (Objects.nonNull(data)) {
            TextRenderData textRenderData = new TextRenderData(data);
            TextRenderPolicy.Helper.renderTextRun(run, textRenderData);
        }
    }

    public TextBlankRenderPolicy() {

    }

    public TextBlankRenderPolicy(String defaultVal) {
        this.defaultVal = defaultVal;
    }
}
